package com.e.dianshang.controller;

import com.e.dianshang.pojo.Goods;
import com.e.dianshang.pojo.GoodsClassOne;
import com.e.dianshang.pojo.GoodsClassThree;
import com.e.dianshang.service.GoodsClassOneService;
import com.e.dianshang.service.GoodsClassThreeService;
import com.e.dianshang.service.GoodsClassTwoService;
import com.e.dianshang.service.GoodsService;
import com.e.dianshang.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: HP
 * Date: 2020/11/10 10:03
 * Description:
 */
@CrossOrigin
@RestController
@Api(value = "执行商品管理的控制器",tags = "商品管理接口的api")
public class GoodsController {

    @Resource
    GoodsClassOneService goodsClassOneService;

    @Resource
    GoodsClassTwoService goodsClassTwoService;

    @Resource
    GoodsClassThreeService goodsClassThreeService;

    @Resource
    GoodsService goodsService;

    JsonResult jsonResult = new JsonResult();

    // 添加商品
    @ApiOperation(value = "商品添加的接口",notes = "添加商品",httpMethod = "POST",response = String.class)
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public Map insert(@RequestBody Goods goods){

        try{
            goodsService.insert(goods);
            jsonResult.setCode("200");
            jsonResult.setMsg("添加成功");
        }catch (Exception e){
            jsonResult.setCode("0");
            jsonResult.setMsg("添加失败");
        }

        return jsonResult.getValues();
    }

    // 修改商品
    @ApiOperation(value = "商品修改的接口",notes = "修改商品",httpMethod = "POST",response = String.class)
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Map update(@RequestBody Goods goods){

        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");

        return jsonResult.getValues();
    }

    // 删除商品（逻辑删除）
    @ApiOperation(value = "商品删除的接口",notes = "删除商品",httpMethod = "GET",response = String.class)
    @RequestMapping(value = "/delete",method =RequestMethod.GET)
    public Map delete(@RequestParam("id") int id){

        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");

        return jsonResult.getValues();
    }

    // 查询所有一级分类
    @ApiOperation(value = "商品一级分类查询的接口",notes = "查询所有一级分类",httpMethod = "GET",response = String.class)
    @RequestMapping(value = "/findAllClassOne",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map findAllClassOne(){

        JsonResult jsonResult = new JsonResult();
        List<GoodsClassOne> allClassOne = goodsClassOneService.findAllClassOne();
        if (allClassOne.size()!=0){
            jsonResult.setData(allClassOne);
            jsonResult.setCode("200");
            jsonResult.setMsg("查询成功");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("该分类下暂时还没有商品哦");
        }
        return jsonResult.getValues();
    }

    // 查询所有二级分类+三级分类
    @ApiOperation(value = "商品二级、三级分类查询的接口",notes = "查询所有二级分类",httpMethod = "GET",response = String.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "YId",value = "一级分类id",required = true, dataType = "int",paramType = "query")})
    @RequestMapping(value = "/findAllClassTwo",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map findAllClassTwo(@RequestParam("YId") int YId){

        JsonResult jsonResult = new JsonResult();
        List<Map> classTwoAndThree = goodsClassTwoService.getClassTwoAndThree(YId);
        if (classTwoAndThree.size()!=0){
            jsonResult.put("classTwo",classTwoAndThree);
            jsonResult.setCode("200");
            jsonResult.setMsg("查询成功");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("该分类下暂时还没有商品");
        }
        return jsonResult.getValues();
    }

    // 查询所有三级分类
    @ApiOperation(value = "商品三级分类查询的接口",notes = "查询所有三级分类",httpMethod = "GET",response = String.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "EId",value = "二级分类id",required = true, dataType = "Integer",paramType = "query")})
    @RequestMapping(value = "/findAllClassThree",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map findAllClassThree(@RequestParam("EId") int EId){

        JsonResult jsonResult = new JsonResult();
        List<GoodsClassThree> allClassThrees = goodsClassThreeService.findAllClassThreeByEId(EId);
        if (allClassThrees.size()!=0){
            jsonResult.setData(allClassThrees);

            jsonResult.setCode("200");
            jsonResult.setMsg("查询成功");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("该分类下还没有商品哦");
        }
        return jsonResult.getValues();
    }

    // 根据商品id查询商品详情
    @ApiOperation(value = "根据商品id查询商品详情的接口",notes = "根据商品id查询商品详情",httpMethod = "GET",response = String.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "商品id",required = true, dataType = "int",paramType = "query")})
    @RequestMapping(value = "/findGoodsDescById",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map findGoodsDesc(@RequestParam("id") int id){

        JsonResult jsonResult = new JsonResult();

        if (id!=0) {
            List<Goods> list01 = goodsService.findGoodsDescById01(id);
            List<Goods> list02 = goodsService.findGoodsDescById02(id);
            List<Goods> list03 = goodsService.findGoodsDescById03(id);
            jsonResult.put("list01", list01);
            jsonResult.put("list02", list02);
            jsonResult.put("list03", list03);
            jsonResult.setCode("200");
            jsonResult.setMsg("查询成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("系统开小差了，请稍后再试");
        }
        return jsonResult.getValues();
    }

    // 根据三级商品id搜索商品
    @ApiOperation(value = "根据三级商品id搜索商品详情的接口",notes = "根据三级商品id查询商品详情",httpMethod = "GET",response = String.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "SId",value = "商品三级id",required = true, dataType = "int",paramType = "query")})
    @RequestMapping(value = "/findByClassThreeId",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map findByClassThreeId(@RequestParam("SId") int SId){

        JsonResult jsonResult = new JsonResult();
        List<Goods> list = goodsService.findByClassThreeId(SId);
        if (list.size()!=0){
            jsonResult.setData(list);
            jsonResult.setCode("200");
            jsonResult.setMsg("查询成功");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("该分类下还没有商品");
        }
        return jsonResult.getValues();
    }


}
